﻿/*
   Copyright 2011 Andrew Davis

    This file is part of Base.

    Base is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Base is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Base.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Base.Prototypes
{
	using System;
	using System.ComponentModel;
	using System.Reflection;

	/// <summary>
	/// Prototype for the derived BaseClasses to follow.
	/// </summary>
	[Serializable()]
	public abstract class BaseClass
	{
		#region "Public Events"
		/// <summary>
		/// This is fired when a property is changed.  The 'PropertyName' of the listener is whatever is passed into the NotifyPropertyChanged method. 
		/// </summary>
		public event PropertyChangedEventHandler PropertyChanged;
		#endregion

		#region "Private Variables"
		/// <summary>
		/// The flag of the current status of the object
		/// </summary>
		protected ObjectStatus objectStatus;
		/// <summary>
		/// The connection string that is passed into it.
		/// </summary>
		protected String connStr;
		#endregion

		#region "Member Varibles"
		/// <summary>
		/// All data structures used by the Base system should have a Int32 primary key.  This is that key. It cannot be overwritten
		/// </summary>
		protected Int32? mID;
		#endregion

		#region "Member Varible Access"
		/// <summary>
		/// Access for mID
		/// </summary>
		public Int32? ID
		{
			get { return mID; }
		}
		#endregion

		#region "Construtors"
		/// <summary>
		/// Generic empty constructor
		/// </summary>
		protected BaseClass() { }
		#endregion

		#region "CRUD"
		/// <summary>
		/// This method is the entry point to the create, update and delete methods.  Which one fires depends on the status of the object.
		/// The properties of the class is picked up and used as parameters for the SQL call.
		/// </summary>
		/// <param name="connStr">The connection string to the data source</param>
		/// <param name="additionalParams">Futher arguments for the SQL call that are not properties of the object</param>
		/// <returns>Whether the database transaction was successful</returns>
		public abstract bool Update(String connStr, object additionalParams = null);
		#region "Database Access"
		#endregion
		#endregion	

		#region "Protected Methods"
		/// <summary>
		/// Updates the ObjectStatus if it needs to and then raises a PropertyChanged event
		/// </summary>
		/// <param name="info">The parameter to flag the listener with.</param>
		protected virtual void NotifyPropertyChanged(String info)
		{
			if (objectStatus == ObjectStatus.Unchanged)
				objectStatus = ObjectStatus.IsDirty;
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(info));
		}
		#endregion

		#region "Public Methods"
		/// <summary>
		/// Flags the object as being ready for deletion 
		/// </summary>
		public virtual void MarkAsDeleted()
		{
			NotifyPropertyChanged("Deleted");
			objectStatus = ObjectStatus.IsOld;
		}
		#endregion
	}
}
